package com.xtianzhuang.www.controller.user.role;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.TdPermission;
import com.xtianzhuang.www.entity.TdPermissionExample;
import com.xtianzhuang.www.entity.TdRole;
import com.xtianzhuang.www.entity.TdRoleExample;
import com.xtianzhuang.www.entity.VRolePermission;
import com.xtianzhuang.www.entity.VRolePermissionExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.QueryRolePermissionRequest;
import com.xtianzhuang.www.entity.request.RoleListQueryRequest;
import com.xtianzhuang.www.entity.request.SaveRoleRequest;
import com.xtianzhuang.www.entity.request.UpdateRolePermissionRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.QueryRolePermissionResponse;
import com.xtianzhuang.www.entity.response.RoleListQueryResponse;
import com.xtianzhuang.www.entity.response.SaveRoleResponse;
import com.xtianzhuang.www.entity.response.UpdateRolePermissionResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/user")
public class RoleController extends BaseController {
	@RequestMapping(value = "/addRole")
	public ModelAndView addRole(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/role-add");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/roleListQuery")
	public ModelAndView userListQuery(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("user/role-list");
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSaveRole")
	@ResponseBody
	@Log(type = LogType.USER)
	public SaveRoleResponse saveRole(SaveRoleRequest request) {
		SaveRoleResponse response = new SaveRoleResponse();
		try {
			userServiceBiz.saveRole(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQueryRoleList")
	@ResponseBody
	@Log(type = LogType.USER)
	public RoleListQueryResponse queryRoleList(RoleListQueryRequest request) {
		RoleListQueryResponse response = new RoleListQueryResponse();
		TdRoleExample example = new TdRoleExample();
		long count = userServiceBiz.roleListCountQuery(example);
		example.setOffset(request.getOffset());
		example.setLimit(request.getLimit());
		List<TdRole> list = userServiceBiz.roleListQuery(example);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		response.setData(list);
		response.setTotal(count);
		return response;
	}

	@RequestMapping("/ajaxQueryRolePermission")
	@ResponseBody
	@Log(type = LogType.USER)
	public QueryRolePermissionResponse queryUserRole(QueryRolePermissionRequest request) {
		QueryRolePermissionResponse response = new QueryRolePermissionResponse();
		TdPermissionExample example = new TdPermissionExample();
		VRolePermissionExample vRolePermissionExample = new VRolePermissionExample();
		vRolePermissionExample.or().andRoleIdEqualTo(request.getRoleId());
		List<TdPermission> list = userServiceBiz.permissionListQuery(example);
		List<VRolePermission> list2 = userServiceBiz.rolePermissionListQuery(vRolePermissionExample);
		List<Integer> list3 = new ArrayList<Integer>(list2.size());
		for (VRolePermission item : list2) {
			list3.add(item.getPermissionId());
		}
		response.setTdPermissions(list);
		response.setvRolePermissions(list3);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		return response;
	}

	@RequestMapping("/ajaxUpdateRolePermission")
	@ResponseBody
	@Log(type = LogType.USER)
	@LogOperate
	public UpdateRolePermissionResponse updateRolePermission(UpdateRolePermissionRequest request) {
		UpdateRolePermissionResponse response = new UpdateRolePermissionResponse();
		userServiceBiz.updateRolePermission(request);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("更新成功");
		return response;
	}
}
